package com.scalpel.spring.util;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

/**
 * 
 * 
 * spring 工具类
 *
 * @author    scalpel.Wang
 * @version   2015年11月5日下午4:24:44
 * @see         // 引用的类
 * Method List: 方法列表                       
 *
 */
public class SpringAppliction implements ApplicationContextAware {

	private static ApplicationContext application;


	public void setApplicationContext(ApplicationContext applicationContext)
			throws BeansException {
		System.out.println("属性注入");
		this.application=applicationContext;
	}


	public final static<T> T getBean(Class<?> c){
		if(application==null||c==null){
			return null;
		}
		T t=null;
		try {

			t= (T)application.getBean(c);
		} catch (BeansException e) {}
		return t;


	}
	public final static<T> T getBean(String beanName, Class<?> requiredType) {
		if(application==null){
			return null;
		}
		return (T)application.getBean(beanName, requiredType);
	}
	public static ApplicationContext getContext() {
		if(application==null){
			return null;
		}
		return application;
	}
	public final static<T> T getBean(String beanName) {
		if(application==null){
			return null;
		}
		return (T)application.getBean(beanName);
	}
}
